package zunge.twidel.typo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import zunge.tool.db.sql.DbConn;

public class TPairPool {
	static String farHost = "brutus.isr.cs.cmu.edu";
	static String farDb = "oneweek";
	static String farUser = "bliu1";
	static String farPswd = "87Clear#";
	
	static final int K = 1;
	
	public static class TPair{
		public String t1;
		public String t2;
		public TPair(String t1, String t2) {
			super();
			this.t1 = t1;
			this.t2 = t2;
			t1.replaceAll("\t|\n", "");
			t2.replaceAll("\t|\n", "");
		}
	}
	
	DbConn farConn;
	List<Long> users;
	int cur = -1;
	List<TPair> userPairs = new ArrayList<TPair>();

	
	public TPairPool(int startPoint){
		this.cur = startPoint;
		try{
			farConn = new DbConn(farHost, farDb, farUser, farPswd);
			users = getUsers();
		}catch(Exception e){e.printStackTrace();}
	}
	public TPair getTweetPair(boolean changeUser) throws Exception{
		//System.err.println("getTweetPair");
		if(changeUser){userPairs.clear();}
		while(userPairs.size() == 0){
			cur++;
			List<Tweet> userTweets =  getUserTweetsSorted(users.get(cur));
			processUser(userTweets, userPairs);
		}
		TPair tpair = userPairs.get(userPairs.size()-1);
		userPairs.remove(userPairs.size()-1);
		return tpair;
	}
	
	void processUser(List<Tweet> tweets, List<TPair> userPairs){
		
		if(tweets == null || tweets.size()==0){return;}
		if(tweets.size()==1){ tweets.get(0).label=TypoCheck.TYPE_NORMAL;return;}
		
		for(int i = 0;i < tweets.size()-1;++i){
			Tweet prev = tweets.get(i);
			if(prev.isDel){
				for(int j = i+1;j < Math.min(i+K+1,tweets.size());++j){
					Tweet curr = tweets.get(j);
					userPairs.add(new TPair(curr.tweetText,prev.tweetText));
				}
			}
		}
	}
	
	
	List<Tweet> getUserTweetsSorted(long userId) throws SQLException, ParseException{
		List<Tweet> tweets = new ArrayList<Tweet>();
		getTweet("select tweetID,createdAt,text from dttweet where user = "+userId, true, tweets);
		getTweet("select tweetID,createdAt,text from undttweet where user = "+userId, false, tweets);
		
		Collections.sort(tweets);
		return tweets;
	}
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
	void getTweet(String query, boolean isd, List<Tweet> tweets) throws SQLException, ParseException{
		ResultSet rs = farConn.executeQuery(query);
		while(rs.next()){
			try{
				tweets.add(new Tweet(rs.getLong("tweetID"), 
					sdf.parse(rs.getString("createdAt")),
					rs.getString("text"), 
					isd));
			}
			catch(ParseException e){
				System.err.println("TweetID="+rs.getLong("tweetID"));
				e.printStackTrace();
			}
		}
		rs.close();
	}

	
	
	static List<Long> getUsers() throws Exception{
		HashSet<Long> users = new HashSet<Long>(150000);
		DbConn farConn = new DbConn(farHost, farDb, farUser, farPswd);
		
		String query = "select distinct user from dttweet";
		ResultSet rs = farConn.executeQuery(query);
		while(rs.next()){
			users.add(rs.getLong("user"));
		}
		rs.close();
		
		//query = "SELECT dttweet.user FROM dttweet INNER JOIN typorepost ON dttweet.tweetID = typorepost.tweetId";
		//rs = farConn.executeQuery(query);
		//while(rs.next()){
		//	users.remove(rs.getLong("user"));
		//}
		//rs.close();
		
		farConn.close();
		
		List<Long> userList = new ArrayList<Long>(users);
		System.out.println("UserCount: "+userList.size());
		return userList;
	}
}
